package com.nofeng.wx.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.util.Map;

@Service
public class TemplateUtils {

    private final TemplateEngine templateEngine;

    @Autowired
    public TemplateUtils(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    /**
     * 渲染HTML模板
     *
     * @param templateName 模板文件名（不含路径和扩展名）
     * @param variables 模板变量
     * @return 渲染后的HTML内容
     */
    public String renderHtml(String templateName, Map<String, Object> variables) {
        Context context = new Context();
        context.setVariables(variables);
        return templateEngine.process(templateName, context);
    }
}